package course.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import course.controller.CourseControllerListener;
import course.po.Dean;

public class DeanJPanel extends JPanel {
	public JTextField searchTextField;
	private static JScrollPane tablePanel;
	private static JPanel panel;
	private DeanJPanel deanJPanel;
	public static JComboBox<String> comboBox;
	public static int isbn = -1;
	private static CourseControllerListener controllerListener;
<<<<<<< HEAD
	public static int deaId;
	public DeanJPanel() {
		this.controllerListener = new CourseControllerListener();
		this.controllerListener.setDeanPanel(this);

		setLayout(null);

		panel = new JPanel();
		panel.setBounds(15, 195, 1020, 387);
		panel.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));

		panel.setLayout(new BorderLayout(0, 0));
		panel.add(getDeanTablePanel(controllerListener.getCourseService().findAllDean()));
		add(panel);
		JPanel paneO = new JPanel();
		paneO.setBounds(15, 15, 1020, 150);
		paneO.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		add(paneO);
		paneO.setLayout(null);

		JLabel searchLabel = new JLabel("搜索类型：");
		searchLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		searchLabel.setBounds(97, 56, 126, 40);
		paneO.add(searchLabel);

		comboBox = new JComboBox<String>();// 创建一个下拉列表框comboBox
		comboBox.addItem("教务员编号");
		comboBox.addItem("教务员姓名");
		comboBox.setBounds(238, 62, 155, 33);
		paneO.add(comboBox);

		searchTextField = new JTextField();
		searchTextField.setBounds(461, 62, 245, 33);
		paneO.add(searchTextField);
		searchTextField.setColumns(10);

		JButton searchDeanButton = new JButton("查询");
		ImageIcon searchDeanIcon = new ImageIcon(DeanJPanel.class.getResource("/searchDean.png"));
		searchDeanButton.setIcon(searchDeanIcon);
		searchDeanButton.setBounds(779, 58, 106, 40);
		paneO.add(searchDeanButton);
		searchDeanButton.setFont(new Font("宋体", Font.PLAIN, 19));
		searchDeanButton.addActionListener(controllerListener);
		searchDeanButton.setActionCommand("searchDeanDialog");

		JPanel paneT = new JPanel();
		paneT.setBounds(15, 610, 1020, 86);
		add(paneT);
		paneT.setLayout(null);
        
		
		JButton updateDeanButton = new JButton("修改");
		ImageIcon updateDeanIcon = new ImageIcon(DeanJPanel.class.getResource("/updateDean.png"));
		updateDeanButton.setIcon(updateDeanIcon);
		updateDeanButton.setBounds(442, 15, 106, 40);
		paneT.add(updateDeanButton);
		updateDeanButton.setFont(new Font("宋体", Font.PLAIN, 19));
		updateDeanButton.addActionListener(controllerListener);
		updateDeanButton.setActionCommand("updateDeanDialog");

		JButton deleteDeanButton = new JButton("删除");
		ImageIcon deleteDeanIcon = new ImageIcon(DeanJPanel.class.getResource("/deleteDean.png"));
		deleteDeanButton.setIcon(deleteDeanIcon);
		deleteDeanButton.setBounds(716, 15, 106, 40);
		paneT.add(deleteDeanButton);
		deleteDeanButton.setFont(new Font("宋体", Font.PLAIN, 19));
		deleteDeanButton.addActionListener(controllerListener);
		deleteDeanButton.setActionCommand("deleteDeanDialog");

		JButton addDeanButton = new JButton("添加");
		ImageIcon addDeanIcon = new ImageIcon(DeanJPanel.class.getResource("/addDean.png"));
		addDeanButton.setIcon(addDeanIcon);
		addDeanButton.setBounds(156, 15, 106, 40);
		paneT.add(addDeanButton);
		addDeanButton.setFont(new Font("宋体", Font.PLAIN, 19));
		addDeanButton.addActionListener(controllerListener);
		addDeanButton.setActionCommand("addDeanDialog");

	}

	private static JScrollPane getDeanTablePanel(List<Dean> deanList) {
		String titles[] = { "编号", "教职工号", "姓名", "性别", "年龄" };
		Object[][] data = new Object[deanList.size()][5];
		Iterator<Dean> iter = deanList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Dean dean = iter.next();
			data[i][0] = dean.getDeaId();
			data[i][1] = dean.getDeaNo();
			data[i][2] = dean.getDeaName();
			data[i][3] = dean.getDeaSex();
			data[i][4] = dean.getDeaAge();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
				 deaId= Integer.valueOf(table.getValueAt(isbn, 0).toString());
=======

	public DeanJPanel() {
		this.controllerListener = new CourseControllerListener();
		this.controllerListener.setDeanPanel(this);

		setLayout(null);

		panel = new JPanel();
		panel.setBounds(15, 195, 1020, 387);
		panel.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));

		panel.setLayout(new BorderLayout(0, 0));
		panel.add(getDeanTablePanel(controllerListener.getCourseService().findAllDean()));
		add(panel);
		JPanel paneO = new JPanel();
		paneO.setBounds(15, 15, 1020, 150);
		paneO.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		add(paneO);
		paneO.setLayout(null);

		JLabel searchLabel = new JLabel("搜索类型：");
		searchLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		searchLabel.setBounds(97, 56, 126, 40);
		paneO.add(searchLabel);

		comboBox = new JComboBox<String>();// 创建一个下拉列表框comboBox
		comboBox.addItem("教务员编号");
		comboBox.addItem("教务员姓名");
		comboBox.setBounds(238, 62, 155, 33);
		paneO.add(comboBox);

		searchTextField = new JTextField();
		searchTextField.setBounds(461, 62, 245, 33);
		paneO.add(searchTextField);
		searchTextField.setColumns(10);

		JButton searchDeanButton = new JButton("查询");
		ImageIcon searchDeanIcon = new ImageIcon(DeanJPanel.class.getResource("/searchDean.png"));
		searchDeanButton.setIcon(searchDeanIcon);
		searchDeanButton.setBounds(779, 58, 106, 40);
		paneO.add(searchDeanButton);
		searchDeanButton.setFont(new Font("宋体", Font.PLAIN, 19));
		searchDeanButton.addActionListener(controllerListener);
		searchDeanButton.setActionCommand("searchDeanDialog");

		JPanel paneT = new JPanel();
		paneT.setBounds(15, 610, 1020, 86);
		add(paneT);
		paneT.setLayout(null);

		JButton updateDeanButton = new JButton("修改");
		ImageIcon updateDeanIcon = new ImageIcon(DeanJPanel.class.getResource("/updateDean.png"));
		updateDeanButton.setIcon(updateDeanIcon);
		updateDeanButton.setBounds(442, 15, 106, 40);
		paneT.add(updateDeanButton);
		updateDeanButton.setFont(new Font("宋体", Font.PLAIN, 19));
		updateDeanButton.addActionListener(controllerListener);
		updateDeanButton.setActionCommand("updateDeanDialog");

		JButton deleteDeanButton = new JButton("删除");
		ImageIcon deleteDeanIcon = new ImageIcon(DeanJPanel.class.getResource("/deleteDean.png"));
		deleteDeanButton.setIcon(deleteDeanIcon);
		deleteDeanButton.setBounds(716, 15, 106, 40);
		paneT.add(deleteDeanButton);
		deleteDeanButton.setFont(new Font("宋体", Font.PLAIN, 19));
		deleteDeanButton.addActionListener(controllerListener);
		deleteDeanButton.setActionCommand("deleteDeanDialog");

		JButton addDeanButton = new JButton("添加");
		ImageIcon addDeanIcon = new ImageIcon(DeanJPanel.class.getResource("/addDean.png"));
		addDeanButton.setIcon(addDeanIcon);
		addDeanButton.setBounds(156, 15, 106, 40);
		paneT.add(addDeanButton);
		addDeanButton.setFont(new Font("宋体", Font.PLAIN, 19));
		addDeanButton.addActionListener(controllerListener);
		addDeanButton.setActionCommand("addDeanDialog");

	}

	private static JScrollPane getDeanTablePanel(List<Dean> deanList) {
		String titles[] = { "编号", "教职工号", "姓名", "性别", "年龄" };
		Object[][] data = new Object[deanList.size()][5];
		Iterator<Dean> iter = deanList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Dean dean = iter.next();
			data[i][0] = dean.getDeaId();
			data[i][1] = dean.getDeaNo();
			data[i][2] = dean.getDeaName();
			data[i][3] = dean.getDeaSex();
			data[i][4] = dean.getDeaAge();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
>>>>>>> refs/remotes/origin/master
			}
		});
		tablePanel = new JScrollPane(table);
		return tablePanel;
	}

	public void showDeanSearchResult(List<Dean> result) {
		panel.remove(tablePanel);
		panel.add(this.getDeanTablePanel(result), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}

	public static void refreshDean(List<Dean> deanList) {
		System.out.println("测试——当前教务员数量为：" + deanList.size());
		panel.remove(tablePanel);
		panel.add(DeanJPanel.getDeanTablePanel(controllerListener.getCourseService().findAllDean()),
				BorderLayout.CENTER);
		panel.validate();
		JOptionPane.showMessageDialog(panel, "重新加载数据成功！");
	}

	public int getDeanIsbn() {
		return isbn;
	}

	public JTextField getSearchTextField() {
		return searchTextField;
	}

	public void setSearchTextField(JTextField searchTextField) {
		this.searchTextField = searchTextField;
	}

	public CourseControllerListener getControllerListener() {
		return controllerListener;
	}
}
